package cn.net.xyan.easy.webapp.base.service.impl;

import cn.net.xyan.easy.gwt.shared.StringUtils;
import cn.net.xyan.easy.webapp.base.annotation.Module;
import cn.net.xyan.easy.webapp.base.api.service.SystemService;
import cn.net.xyan.easy.webapp.base.dao.PermissionDao;
import cn.net.xyan.easy.webapp.base.dao.SystemUserDao;
import cn.net.xyan.easy.webapp.base.dao.WebModuleDao;
import cn.net.xyan.easy.webapp.base.api.dto.QueryWebModuleRsq;
import cn.net.xyan.easy.webapp.base.api.dto.RestService;
import cn.net.xyan.easy.webapp.base.entity.Permission;
import cn.net.xyan.easy.webapp.base.entity.SystemUser;
import cn.net.xyan.easy.webapp.base.entity.WebModule;
import cn.net.xyan.easy.webapp.base.service.IDTOService;
import cn.net.xyan.easy.webapp.base.service.ISecurityService;
import cn.net.xyan.easy.webapp.base.service.IWebModuleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by xiashenpin on 16/2/9.
 */
public class DefaultWebModuleService implements IWebModuleService,SystemService {

    @Autowired
    WebModuleDao moduleDao;

    @Autowired
    PermissionDao permissionDao;

    @Autowired
    ISecurityService securityService;

    @Autowired
    IDTOService dtoService;

    @Autowired
    SystemUserDao userDao;

    List<RestService> restServices = new LinkedList<>();



    public WebModule setupModule(WebModule parent, String name, String title,String url,Integer order, List<Permission> permissions) {
        WebModule module = moduleDao.findOne(name);
        if (module == null){
            module = new WebModule();
            module.setName(name);
            module.setTitle(title);
            String baseURL = "";
            if (parent!=null)
                baseURL = parent.getUrl();
            module.setUrl(baseURL+url);
            module.setOrderIndex(order);
            for (Permission p : permissions){
                module.getRequiredPermission().add(p);
            }
            module.setParent(parent);
            module = moduleDao.saveAndFlush(module);
        }
        return module;
    }

    @Override
    public WebModule setupModule(WebModule parent, String name,String url,Module module) {
        List<Permission> permissions = new LinkedList<>();
        for (String p:module.requiredPermissions()){
            permissions.add(permissionDao.findByPermission(p));
        }
        return setupModule(parent,name,module.title(),url,module.order(),permissions);
    }

    @Override
    public RestService setupRestService(String baseURL, String name, String url) {
        RestService restService = new RestService();
        restService.setName(name);
        restService.setUrl(baseURL+url);
        restServices.add(restService);
        return restService;
    }

    @Override
    public List<RestService> allRestService() {
        return restServices;
    }

    @Override
    public List<QueryWebModuleRsq> queryModuleForUser(SystemUser user) {
        List<QueryWebModuleRsq> webModuleRsqs = new LinkedList<>();
        for (WebModule wm : moduleDao.rootModules()){
            if (securityService.checkPermissionForUser(user,wm.getRequiredPermission())){
                QueryWebModuleRsq webModuleRsq = dtoService.convert(QueryWebModuleRsq.class,wm);
                webModuleRsqs.add(webModuleRsq);

                for (WebModule item : wm.getItems()){
                    if (securityService.checkPermissionForUser(user,item.getRequiredPermission())) {
                        QueryWebModuleRsq itemWebModuleRsq = dtoService.convert(QueryWebModuleRsq.class,item);
                        webModuleRsq.getItems().add(itemWebModuleRsq);
                    }
                }

            }
        }
        return webModuleRsqs;
    }

    @Override
    public List<QueryWebModuleRsq> queryModuleForUser(String username) {
        SystemUser systemUser = null;
        if (username!=null){
            systemUser= userDao.findOne(username);
        }
        if (systemUser == null)
            systemUser = securityService.currentUser();
        return queryModuleForUser(systemUser);
    }
}
